package org.black.phoneUtility.sms.schedule.app.fragment;

import org.black.phoneUtility.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.NumberPicker;

public class SmsScheduleRepeatEndAfterDialogFragment extends DialogFragment {
	public static final String TAG = SmsScheduleRepeatEndAfterDialogFragment.class
			.getName();
	public static final String LOG_TAG = SmsScheduleRepeatEndAfterDialogFragment.class
			.getName();
	public static final String ARGUMENTS_REPEAT_END_AFTER_TIMES = "arguments_repeat_end_after_times";

	public interface SmsScheduleRepeatEndAfterDialogInterface {
		public void onSmsScheduleRepeatEndAfterDialogPositiveClick(int number);

		public void onSmsScheduleRepeatEndAfterDialogNegativeClick();
	}

	private SmsScheduleRepeatEndAfterDialogInterface smsScheduleRepeatEndAfterDialogInterface;

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		try {
			smsScheduleRepeatEndAfterDialogInterface = (SmsScheduleRepeatEndAfterDialogInterface) activity;
		} catch (ClassCastException e) {
			throw new ClassCastException(
					activity.toString()
							+ " must implement SmsScheduleRepeatEndAfterDialogInterface");
		}
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		super.onCreateDialog(savedInstanceState);
		Bundle bundle = getArguments();
		int endAfterTimes = bundle.getInt(ARGUMENTS_REPEAT_END_AFTER_TIMES);
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
		Log.d(LOG_TAG, "endAfterTimes: " + endAfterTimes);

		LayoutInflater inflater = getActivity().getLayoutInflater();
		View view = inflater.inflate(
				R.layout.sms_schedule_repeat_end_after_dialog, null);
		NumberPicker numberPicker = (NumberPicker) view
				.findViewById(R.id.smsScheduleRepeatEndAfterNumberPicker);
		numberPicker.setMinValue(2);
		numberPicker.setMaxValue(50);
		numberPicker.setValue(endAfterTimes);
		builder.setView(view)
				.setPositiveButton(android.R.string.ok,
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								smsScheduleRepeatEndAfterDialogInterface
										.onSmsScheduleRepeatEndAfterDialogPositiveClick(((NumberPicker) getDialog()
												.findViewById(
														R.id.smsScheduleRepeatEndAfterNumberPicker))
												.getValue());

							}
						})
				.setNegativeButton(android.R.string.cancel,
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {

							}
						});
		return builder.create();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		this.smsScheduleRepeatEndAfterDialogInterface = null;
	}

}
